package com.xm.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * github：https://github.com/quartz-scheduler/quartz
 */
public class QuartzDemo {
    public static void main(String[] args) {
        try {
            //1、创建 Scheduler 工厂（搭配quartz文件配置使用）
            StdSchedulerFactory schedulerFactory = new StdSchedulerFactory("quartz.properties");
            Scheduler scheduler = schedulerFactory.getScheduler();
            //2、创建 JobDetail（绑定具体 Job）
            JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                    .withIdentity("myJob", "group1")
                    .storeDurably(true)
                    .build();
            //3、创建 Trigger（定义执行规则）
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity("myTrigger", "group1")
//                    .startNow()
//                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
//                    .build();

            Trigger trigger = TriggerBuilder.newTrigger()
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                    .build();
            //4、将 Job 和 Trigger 注册到 Scheduler 工厂
            scheduler.scheduleJob(jobDetail, trigger);
            //5、启动调度器
            scheduler.start();
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        } finally {
        }
    }
}
